<template>
  <!-- 新增优惠价 -->
  <el-dialog
    :title="idname"
    :visible="showadd"
    width="40%"
    center
    @close="btncancel"
  >
    <el-row>
      <span>
        新增优惠价
      </span>
    </el-row>
    <br />
    <br />
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="用户名" prop="name">
        <el-input
          v-model="form.name"
          autocomplete="off"
          style="width: 550px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          autocomplete="off"
          style="width: 550px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input
          v-model="form.phone"
          autocomplete="off"
          style="width: 550px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="职称" prop="position">
        <el-input
          v-model="form.position"
          autocomplete="off"
          style="width: 550px;"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-tree
          show-checkbox
          node-key="id"
          :default-expanded-keys="[2, 3]"
          :default-checked-keys="[5]"
          style="width: 300px;"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="btncancel">取 消</el-button>
      <el-button type="primary" @click="addisok">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { editsetting, addsettin } from '@/api/setting'
export default {
  name: 'hehe',
  props: {
    showadd: {
      type: Boolean,
      default: false
    },
    dates: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '', // 账号
        password: '', // 密码
        phone: '', // 手机号码
        position: '' // 职称//权限
      },

      //   表单校验
      rules: {
        name: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位'
          }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码！' },
          {
            mai: '6',
            max: '12',
            message: '密码格式不对！'
          }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        position: [
          { required: true, message: '职称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '职称为1-4位'
          }
        ]
      }
    }
  },
  computed: {
    idname() {
      return this.form.id ? '编辑店铺' : '新增店铺'
    }
  },
  created() {
    // 存储的数据
    this.$nextTick(() => {
      console.log(this.dates)
      this.form = this.dates
    })
  },
  methods: {
    // 点击确定
    async addisok() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          // 编辑操作
          await editsetting(this.form)
          this.$parent.dialogVisible = false
        } else {
          //新增操作
          await addsettin(this.form)
          this.$parent.dialogVisible = false
        }
        this.$message.success('操作成功！')
      } catch (error) {
        console.log(error)
      }
    },
    //点击关闭 清除数据
    btncancel() {
      this.form = {
        name: '', // 账号
        password: '', // 密码
        phone: '', // 手机号码
        position: '' // 职称
        //权限
      }
      this.$nextTick(() => {
        this.$refs.form.resetFields() //清空表单
      })
      this.$parent.dialogVisible = false
    }
  }
}
</script>
<style lang=""></style>
